package stardust.core.support;


import java.math.BigInteger;



public class ByteUtils
{
  
  public static boolean equalsByteArray(byte[] x, byte[] y)
  {
    if (x == y)
    {
      return true;
    }
    else if ((x == null && y != null) || (x != null && y == null) || x.length != y.length)
    {
      return false;
    }
    else
    {
      for (int i = 0; i < x.length; i++)
      {
        if (x[i] != y[i])
        {
          return false;
        }
      }
      
      return true;
    }
  }

  public static byte[] subByteArray(byte[] b, int offset, int len)
  {
    byte[] buffer = new byte[len];

    System.arraycopy(b, offset, buffer, 0, len);

    return buffer;
  }

  public static byte[] trimByteArray(byte[] b)
  {
    int offset = 0;

    for (int i = 0; i < b.length; i++)
    {
      if (b[i] == 0)
      {
        offset = i;
        break;
      }
    }

    return offset == 0 ? b : ByteUtils.subByteArray(b, 0, offset);
  }

  public static byte[] formatByteArray(byte[] b, int len)
  {
    if (b.length > len)
    {
      return ByteUtils.subByteArray(b, 0, len);
    }
    else if (b.length < len)
    {
      byte[] tb = new byte[len - b.length];

      for (int i = 0; i < tb.length; i++)
      {
        tb[i] = 0;
      }

      b = ByteUtils.getBytes(new byte[][]
      {
        b, tb
      });
    }

    return b;
  }
  
  /*
  public static int byteToInt(byte[] b)
  {
    return new BigInteger(b).intValue();
  }
  
  public static byte[] intToByte(int n, int len)
  {
    byte[] b = BigInteger.valueOf(n).toByteArray();
    
    if (b.length > len)
    {
      throw new IllegalArgumentException("将整数转换为字节序时溢出。");
    }
    else
    {
      return ByteUtils.getBytes(new byte[len - b.length], b);
    }
  }
  */

  public static int byteToInt(byte[] b)
  {
    int r = 0;

    for (int i = 0; i < 4 && i < b.length; i++)
    {
      r <<= 8;
      r |= b[i] & 0xFF;
    }

    return r;
  }

  public static byte[] intToByte(int n, int len)
  {
    byte[] b = new byte[len];

    for (int i = 0; i < b.length; i++)
    {
      b[b.length - i - 1] = (byte) (n >>> (i * 8));
    }

    return b;
  }

  public static String toHexMac(byte[] data)
  {
    String mac = "";
    String b;

    for (int i = 0; i < data.length; i++)
    {

      b = Integer.toHexString(data[i] & 0xFF);
      mac += (b.length() == 1 ? "0" : "") + b;
      mac += i < data.length - 1 ? "-" : "";
    }

    return mac;
  }

  public static byte[] toBinaryMac(String mac)
  {
    String[] macBuffer = mac.split("-");
    byte[] data = new byte[6];

    for (int i = 0; i < 6; i++)
    {
      data[i] = (byte) Integer.parseInt(macBuffer[i], 16);
    }

    return data;
  }

  public static String toDecimalIp(byte[] data)
  {
    String mac = "";

    for (int i = 0; i < data.length; i++)
    {

      mac += data[i] & 0xFF;
      mac += i < data.length - 1 ? "." : "";
    }

    return mac;
  }

  public static byte[] toBinaryIp(String ip)
  {
    String[] ipBuffer = ip.split("[.]");
    byte[] data = new byte[4];

    for (int i = 0; i < 4; i++)
    {
      data[i] = (byte) Integer.parseInt(ipBuffer[i], 10);
    }

    return data;
  }

  public static byte[] getBytes(byte[]... data)
  {
    byte[] buffer;
    int length = 0;

    for (int i = 0; i < data.length; i++)
    {
      length += data[i].length;
    }

    buffer = new byte[length];
    length = 0;

    for (int i = 0; i < data.length; i++)
    {
      System.arraycopy(data[i], 0, buffer, length, data[i].length);
      length += data[i].length;
    }

    return buffer;
  }

  public static void main(String[] args)
  {
    int i = 0;

    System.out.println(i += 1);
    // byte[] etherHardtype = ByteUtils.intToByte(2054, 2);
    // System.out.println(ByteUtils.byteToInt(etherHardtype));
    //
    // etherHardtype = ByteUtils.intToByte(2048, 2);
    // System.out.println(ByteUtils.byteToInt(etherHardtype));
    //
    //
    // etherHardtype.toString();
    // 10.210.29.150 00-0a-e4-38-b2-f9
    // byte[] ip = ByteUtils.toBinaryIp("10.210.29.150");
    //
    // System.out.println(ByteUtils.toDecimalIp(ip));
    //
    // byte[] mac = ByteUtils.toBinaryMac("00-0a-e4-38-b2-f9");
    //
    // System.out.println(ByteUtils.toHexMac(mac));
  }

}
